package com.cxt.autoFill;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.cxt.context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MymetaObjecthandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段填充insert");
        log.info(metaObject.toString());
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        //TODO：测试环境下没有登录记录Id，暂时写死
        metaObject.setValue("createUser", BaseContext.getCurrentId() != null ? BaseContext.getCurrentId() : 1086L);
        metaObject.setValue("updateUser", BaseContext.getCurrentId() != null ? BaseContext.getCurrentId() : 1086L);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段填充update");
        log.info(metaObject.toString());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId() != null ? BaseContext.getCurrentId() : 1086L);
    }
}
